﻿
///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: MobiMaterialsProcurementApproval.cs
/// 作    者: HYG
/// 创建日期: 2013/1/12 9:48:18
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 手机端实现
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.WebApp.Crm.Workflow
{
    using System;
    using ZX.BPM.Library;
    using DevExpress.Web.ASPxEditors;
    using System.Web.Caching;
    using System.Data;
    using System.Web;
    /// <summary>
    /// 采购审批单
    /// </summary>
    public partial class MobiMaterialsProcurementApproval : ZX.Web.UI.Controls.EditPage
    {
        #region 页面加载事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["User"] == null)
            {
                Response.Redirect("../../Login.aspx");
            }
            if (!this.IsPostBack)
            {
                this.Session["NeedsOfBorrower" + strWorkflowInsId] = null;

                NeedsOfBorrowerEdit entity = GetNeedsOfBorrowerEdit();

                //设置数据源
                NeedsOfBorrower.Bind(entity, true);
                NeedsOfBorrower.DataBind();

                NeedsOfBorrower.InitFileUploader(entity);

                MaterialsProcurementInfo.Bind(entity, true);
                MaterialsProcurementInfo.DataBind();


                //设置采购单信息
                MaterialsProcurementDetail.DataSource = entity;
                MaterialsProcurementDetail.DataBind();

                //设置只读
                NeedsOfBorrower.ReadOnly = true;
                MaterialsProcurementDetail.ReadOnly = true;
                MaterialsProcurementInfo.ReadOnly = true;
            }

            if (Status == "3")
            {
                ApprovalControl.ReadOnly = true;
                btnReturn.Enabled = false;
                btnSubmit.Enabled = false;
            }
            if (Cache["mobi-deptlist"] == null)//缓存部门列表
            {
                DeptHtml = (new ZX.Web.UI.Controls.UserControl.DeptList()).GetDeptHtml();
                Cache.Insert("mobi-deptlist", DeptHtml, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
            }
            else
            {
                DeptHtml = (string)Cache["mobi-deptlist"];
            }
        }

        #endregion

        #region 事件处理

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ApprovalControl.SaveUserControlApp(
                   this.User.LoginName,
                   this.User.UName,
                   this.EmpPropertyValues.DeptName,
                   this.EmpPropertyValues.PostName,
                   strOperatorInstanceId,
                   strWorkFlowId,
                   strWorkflowInsId,
                   strWorkTaskId,
                   strWorkTaskInsId);

                ASPxButton button = (ASPxButton)sender;
                ApprovalControl.Submit(
                    this.User.LoginName,
                    strOperatorInstanceId,
                    button.Text);


                hdfback.Value = getWFTooltip(ApprovalControl.GetRedirectID(strWorkTaskInsId, strOperatorInstanceId));
            }
            catch (Exception ex)
            {
                ZX.BLL.Extend.LogBusiness.WriteExceptionLog(ex, this.User);
            }
        }

        #endregion

        #region 查询参数
        public string DeptHtml
        { get; set; }
        public string Status
        { get { return this.GetStringFromQueryString("statues", ""); } }


        /// <summary>
        /// 
        /// </summary>
        public string strWorkFlowId
        {
            get
            {
                string wtid = this.GetStringFromQueryString("WorkflowId", "");
                if (wtid == "")
                {
                    wtid = this.GetStringFromQueryString("wfid", "");
                }
                return wtid;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        {
            get
            {
                string wfid = this.GetStringFromQueryString("WorktaskId", "");
                if (wfid == "")
                {
                    wfid = this.GetStringFromQueryString("wtid", "");
                }
                return wfid;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        public string strOperatorInstanceId
        { get { return this.GetStringFromQueryString("OperatorInstanceId", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkflowInsId
        { get { return this.GetStringFromQueryString("WorkflowInsId", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskInsId
        { get { return this.GetStringFromQueryString("WorktaskInsId", ""); } }

        public string strView
        { get { return this.GetStringFromQueryString("view", ""); } }
        public string strOperStatus
        { get { return this.GetStringFromQueryString("OperStatus", ""); } }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }

        #endregion

        #region 本地方法

        private NeedsOfBorrowerEdit GetNeedsOfBorrowerEdit()
        {
            NeedsOfBorrowerEdit businessObject = (NeedsOfBorrowerEdit)Session["NeedsOfBorrower" + strWorkflowInsId];
            if (businessObject == null)
            {
                try
                {

                    if (!string.IsNullOrEmpty(strWorkflowInsId))
                    {
                        businessObject = NeedsOfBorrowerEdit.Get(strWorkflowInsId);
                        //businessObject.WorkTaskInsId = strWorkTaskInsId;
                        //businessObject.WorkTaskId = strWorkTaskId;
                    }
                    else
                    {
                        businessObject = NeedsOfBorrowerEdit.New();
                        businessObject.CreatedTime = DateTime.Now.ToShortDateString();
                        businessObject.Proposer = base.UserName;
                        businessObject.PostName = base.EmpPropertyValues.PostName;
                        businessObject.Filiale = base.EmpPropertyValues.Filiale;
                        businessObject.DeptName = base.EmpPropertyValues.DeptName;
                    }
                    Session["NeedsOfBorrower" + strWorkflowInsId] = businessObject;
                }
                catch (System.Security.SecurityException)
                {

                }
            }

            return businessObject;
        }

        #endregion


        #region mobi

        private string getWFTooltip(string WorkTaskInstanceId)
        {
            ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient();
            WFTooltip tooltip = new WFTooltip();
            if (!string.IsNullOrEmpty(WorkTaskInstanceId))
            {
                string title = "";
                string TaskToWhoMsg = "";
                string ResultMsg = "";

                TaskToWhoMsg = client.GetTaskToWhoMsg(WorkTaskInstanceId);
                //string transmitId = Session["transmitId"] == null ? string.Empty :Session["transmitId"].ToString();    //todo zhuyun
                string transmitId = ApprovalControl.txtTransmitId.Value;
                ResultMsg = client.GetResultMsg(WorkTaskInstanceId);
                DataTable dt = client.GetWorkTaskInstanceView(WorkTaskInstanceId);

                title = "操作结果:" + ResultMsg;

                if (TaskToWhoMsg.Length <= 0)
                {
                    TaskToWhoMsg = "未提交至任何人,请检查流程模板和组织机构配置是否正确!";
                    if (ResultMsg == WorkFlowConst.WorkflowOverMsg)//流程结束
                    {
                        TaskToWhoMsg = "流程结束!";
                    }
                    if (ResultMsg == WorkFlowConst.TaskBackMsg)
                    {
                        TaskToWhoMsg = WorkFlowConst.TaskBackMsg;
                    }
                }

                else
                {
                    if (!string.IsNullOrEmpty(transmitId))
                    {
                        string[] transmituser = transmitId.Split(',');
                        string[] username = TaskToWhoMsg.Split(',');
                        TaskToWhoMsg = "";
                        for (int j = 0; j < transmituser.Length; j++)
                        {
                            for (int i = 0; i < username.Length; i++)
                            {
                                if (transmituser[j].ToString() == username[i].ToString())
                                {
                                    TaskToWhoMsg += username[i].ToString();
                                }
                            }
                        }
                    }

                    //Session["transmitId"] = null;
                    TaskToWhoMsg = "成功提交至:" + TaskToWhoMsg + "。你已完成该任务处理,可以关闭该窗口。";
                }
                //---------------------------todo zhuyun 20121012--------------------------//
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["OperType"].ToString() == "12")
                    {
                        TaskToWhoMsg = "已成功提交,你已完成该任务处理,可以关闭该窗口。";
                    }
                }
                if (title.Contains("退回"))
                {
                    TaskToWhoMsg = "成功退回至提交人！";
                }

                tooltip.title = title;
                tooltip.TaskToWhoMsg = TaskToWhoMsg;
                tooltip.ResultMsg = ResultMsg;

            }

            return tooltip.title + "<br>" + tooltip.TaskToWhoMsg + "<br>" + tooltip.ResultMsg;
        }
        /// <summary>
        /// 流程任务信息提示实体
        /// </summary>
        [Serializable]
        public class WFTooltip
        {
            /// <summary>
            /// 标题
            /// </summary>
            public string title
            {
                get;
                set;
            }
            /// <summary>
            /// 任务信息
            /// </summary>
            public string TaskToWhoMsg
            {
                get;
                set;
            }
            /// <summary>
            /// 操作结果
            /// </summary>
            public string ResultMsg
            {
                get;
                set;
            }

        }

        #endregion
    }
}